package com.sinotrans.common.rest;
/*
 * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;

import io.swagger.annotations.ApiModelProperty;

/**
 * 简单分页模型
 *
 * @author hubin
 * @since 2018-06-09
 */
public class Page<T> implements IPage<T> {

	private static final long serialVersionUID = 8545996863226528798L;

	/**
	 * 查询数据列表
	 */
	@ApiModelProperty(value = "查询数据列表")
	protected List<T> records = Collections.emptyList();

	/**
	 * 总数
	 */
	@ApiModelProperty(value = "总数")
	protected long total = 0;
	/**
	 * 每页显示条数，默认 10
	 */
	@ApiModelProperty(value = "每页显示条数，默认 10")
	protected long size = 10;

	/**
	 * 当前页
	 */
	@ApiModelProperty(value = "当前页")
	protected long current = 1;

	/**
	 * 排序字段信息
	 */
	@ApiModelProperty(value = "排序字段信息")
	protected List<OrderItem> orders = new ArrayList<>();

	/**
	 * 自动优化 COUNT SQL
	 */
	protected boolean optimizeCountSql = true;
	/**
	 * 是否进行 count 查询
	 */
	@ApiModelProperty(value = "是否进行 count 查询")
	protected boolean isSearchCount = true;
	/**
	 * 是否命中count缓存
	 */
	protected boolean hitCount = false;
	/**
	 * countId
	 */
	protected String countId;
	/**
	 * countId
	 */
	protected Long maxLimit;

	public Page() {
	}

	/**
	 * 分页构造函数
	 *
	 * @param current 当前页
	 * @param size    每页显示条数
	 */
	public Page(long current, long size) {
		this(current, size, 0);
	}

	public Page(long current, long size, long total) {
		this(current, size, total, true);
	}

	public Page(long current, long size, boolean isSearchCount) {
		this(current, size, 0, isSearchCount);
	}

	public Page(long current, long size, long total, boolean isSearchCount) {
		if (current > 1) {
			this.current = current;
		}
		this.size = size;
		this.total = total;
		this.isSearchCount = isSearchCount;
	}

	/**
	 * 是否存在上一页
	 *
	 * @return true / false
	 */
	public boolean hasPrevious() {
		return this.current > 1;
	}

	/**
	 * 是否存在下一页
	 *
	 * @return true / false
	 */
	public boolean hasNext() {
		return this.current < this.getPages();
	}

	@Override
	public List<T> getRecords() {
		return this.records;
	}

	@Override
	public Page<T> setRecords(List<T> records) {
		this.records = records;
		return this;
	}

	@Override
	public long getTotal() {
		return this.total;
	}

	@Override
	public Page<T> setTotal(long total) {
		this.total = total;
		return this;
	}

	@Override
	public long getSize() {
		return this.size;
	}

	@Override
	public Page<T> setSize(long size) {
		this.size = size;
		return this;
	}

	@Override
	public long getCurrent() {
		return this.current;
	}

	@Override
	public Page<T> setCurrent(long current) {
		this.current = current;
		return this;
	}

	@Override
	public String countId() {
		return getCountId();
	}

	@Override
	public Long maxLimit() {
		return getMaxLimit();
	}

	/**
	 * 添加新的排序条件，构造条件可以使用工厂：{@link OrderItem#build(String, boolean)}
	 *
	 * @param items 条件
	 * @return 返回分页参数本身
	 */
	public Page<T> addOrder(OrderItem... items) {
		orders.addAll(Arrays.asList(items));
		return this;
	}

	/**
	 * 添加新的排序条件，构造条件可以使用工厂：{@link OrderItem#build(String, boolean)}
	 *
	 * @param items 条件
	 * @return 返回分页参数本身
	 */
	public Page<T> addOrder(List<OrderItem> items) {
		orders.addAll(items);
		return this;
	}

	@Override
	public List<OrderItem> orders() {
		return getOrders();
	}

	@Override
	public boolean optimizeCountSql() {
		return optimizeCountSql;
	}

	public boolean isOptimizeCountSql() {
		return optimizeCountSql();
	}

	@Override
	public boolean isSearchCount() {
		if (total < 0) {
			return false;
		}
		return isSearchCount;
	}

	public Page<T> setSearchCount(boolean isSearchCount) {
		this.isSearchCount = isSearchCount;
		return this;
	}

	public Page<T> setOptimizeCountSql(boolean optimizeCountSql) {
		this.optimizeCountSql = optimizeCountSql;
		return this;
	}

	public void setHitCount(boolean hit) {
		this.hitCount = hit;
	}

	public List<OrderItem> getOrders() {
		return orders;
	}

	public void setOrders(List<OrderItem> orders) {
		this.orders = orders;
	}

	public String getCountId() {
		return countId;
	}

	public void setCountId(String countId) {
		this.countId = countId;
	}

	public Long getMaxLimit() {
		return maxLimit;
	}

	public void setMaxLimit(Long maxLimit) {
		this.maxLimit = maxLimit;
	}
}
